package br.gov.cemaden.library.model.previsao;

import br.gov.cemaden.library.model.Municipio;
import br.gov.cemaden.library.repository.previsao.PrevisaoPrecipitacaoRepository;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 *
 * @author alan.oliveira
 */
@Entity
@Table(name = "previsao_precipitacao", schema = "previsao")
@SequenceGenerator(name = "sequence", sequenceName = "previsao.previsao_precipitacao_sequence", schema = "previsao")
public class PrevisaoPrecipitacao implements Serializable {
    
    @Id
    @GeneratedValue(generator = "sequence", strategy = GenerationType.AUTO)
    @Column(name = "id_previsao_precipitacao")
    private Long id;
    
    @Temporal(TemporalType.TIMESTAMP)
    private Date datahora;
    
    private Double latitude;
    
    private Double longitude;
    
    private BigDecimal valor;
    
    @ManyToOne(optional = false)
    @JoinColumn(name = "id_municipio", referencedColumnName = "id_municipio", nullable = false)
    private Municipio municipio;
    
    @ManyToOne(optional = false)
    @JoinColumn(name = "id_previsao_precipitacao_aquisicao", referencedColumnName = "id_previsao_precipitacao_aquisicao")
    private AquisicaoPrecipitacao aquisicao;
    
    @Transient
    private PrevisaoPrecipitacaoRepository repository;

    public PrevisaoPrecipitacao() {
    }        

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getDatahora() {
        return datahora;
    }

    public void setDatahora(Date datahora) {
        this.datahora = datahora;
    }

    public Double getLatitude() {
        return latitude;
    }

    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    public Double getLongitude() {
        return longitude;
    }

    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }        

    public BigDecimal getValor() {
        return valor;
    }

    public void setValor(BigDecimal valor) {
        this.valor = valor;
    }

    public Municipio getMunicipio() {
        return municipio;
    }

    public void setMunicipio(Municipio municipio) {
        this.municipio = municipio;
    }

    public AquisicaoPrecipitacao getAquisicao() {
        return aquisicao;
    }

    public void setAquisicao(AquisicaoPrecipitacao aquisicao) {
        this.aquisicao = aquisicao;
    }    

    @Override
    public String toString() {
        return String.format("%td/%tm/%ty - %f;%f = %f", datahora, datahora, datahora, latitude, longitude, valor);
    }        
    
    public PrevisaoPrecipitacaoRepository getRepository(EntityManager em) {
        if(repository == null)
            this.setRepository(new PrevisaoPrecipitacaoRepository(em));
        return repository;
    }

    public void setRepository(PrevisaoPrecipitacaoRepository repository) {
        this.repository = repository;
    }

    public PrevisaoPrecipitacao update(EntityManager em) {
        if(em.isOpen())
            return getRepository(em).update(this);
        else
            throw new NullPointerException("The database connection is closed");
    }
    
}
